package com.itheima.health.jobs;


import com.alibaba.fastjson.JSON;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Slf4j
public class ClearSetmealSetting {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetMealService setMealService;

    public void clearSelectSetMaler(){
        log.info("[每天凌晨2点获取数据库信息]");
        //连接redis
        Jedis jedis = jedisPool.getResource();
        //获取redis数据
        String restList = jedis.get("list");
        System.out.println("restList = " + restList);
        if(restList == null){
            //redis中数据不存在去数据库查询
            List<Setmeal> list = setMealService.findAll();
            System.out.println("list = " + list);
            //将查询到的数据转成字符串存入redis
            jedis.set("list", JSON.toJSON(list).toString());
        }else{
            //数据存在则删除
            Long del = jedis.del("list");
        }
    }
}
